
from datetime import datetime
from app.models.simulation_result import SimulationResult
from app.schemas.simulation_result import SimulationResultCreate
from typing import List, Optional

async def create_simulation_result(data: SimulationResultCreate) -> SimulationResult:
    return await SimulationResult.create(**data.dict())

async def get_simulation_result_by_id(result_id: int) -> Optional[SimulationResult]:
    return await SimulationResult.get_or_none(simulation_result_id=result_id)

async def list_simulation_results() -> List[SimulationResult]:
    return await SimulationResult.all().order_by("time_point")

async def delete_simulation_result(result_id: int) -> int:
    return await SimulationResult.filter(simulation_result_id=result_id).delete()

async def list_by_env_param(env_param_id: int) -> List[SimulationResult]:
    return await SimulationResult.filter(env_param_id=env_param_id).order_by("time_point")

async def list_by_env_param_and_time(env_param_id: int, start: datetime, end: datetime) -> List[SimulationResult]:
    return await SimulationResult.filter(
        env_param_id=env_param_id,
        time_point__gte=start,
        time_point__lte=end
    ).order_by("time_point")